import BaseController from "../../cfw/mvc/BaseController";
import CristmasLightModel from "./model/CristmasLightModel";
import CristmasToyModel from "./model/CristmasToyModel";
import LobbyController from "../lobby/LobbyController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import { UIIndex, ItemState } from "../../cfw/tools/Define";
import CristmasTreeManager from "./model/CristmasTreeManager";
import ItemController from "../item/ItemController";
import BaseItemModel from "../../cfw/mvc/BaseItemModel";
import { ItemType, GameText } from "../../public/Config";
import BagManager from "../bag/BagManager";
import { ResultState, SDKEventName } from "../../sdk/SDKConfig";
import TipController from "../tip/TipController";
import SDKManager from "../../sdk/SDKManager";

export default class CristmasTreeController extends BaseController implements ItemController {

    private static ins: CristmasTreeController;

    static instance() {
        if (!this.ins) {
            this.ins = new CristmasTreeController()
        }
        return this.ins;
    }

    back() {
        LobbyController.instance().intoLayer()
    }

    intoLayer() {
        ModuleManager.setModuleID(ModuleID.DECORATION)
        this.pushView('prefabs/CristmasTreeRoom', 'CristmasTreeRoom', CristmasTreeManager.instance(), ModuleManager.getLoader(), UIIndex.ROOT)
    }

    onButtonIconClick(model: any) {
        if (!model.isOpen()) {
            TipController.instance().showToast(GameText.ITEM_TOAST, { num: model.getOpenLevel() })
            return;
        }
        if (model.getType() == ItemType.LIGHT) {
            this.selectLight(model)
        } else {
            this.selectToy(model)
        }
    }
    private tempLight: CristmasLightModel;
    selectLight(model: CristmasLightModel) {
        if (model.needBuy()) {
            let cost: number[] = model.getCostItem();
            if (BagManager.isEnough(cost[0], cost[1])) {
                BagManager.costItem(cost[0], cost[1], (s) => {
                    if (s == ResultState.YES) {
                        model.setState(ItemState.GOT)
                    }
                })
            }
        } else {
            this.useLight(model)
        }
    }

    useLight(model) {
        if (this.tempLight != model) {
            this.tempLight = model;
            model.use();

        } else {
            this.tempLight.unuse();
            this.tempLight = null;
        }
    }
    private tempToy: CristmasToyModel;
    selectToy(model: CristmasToyModel) {
        SDKManager.getChannel().sendEvent(SDKEventName.tree, model.getID())
        if (model.needBuy()) {
            let cost: number[] = model.getCostItem();
            if (BagManager.isEnough(cost[0], cost[1])) {
                BagManager.costItem(cost[0], cost[1], (s) => {
                    if (s == ResultState.YES) {
                        model.setState(ItemState.GOT)
                    }
                })
            }
        } else {
            this.useToy(model)
        }
    }

    useToy(model: CristmasToyModel) {
        if (this.tempToy != model) {
            this.tempToy = model;
            model.use();

        } else {
            this.tempToy.unuse();
            this.tempToy = null;
        }
    }
}
